package com.mmp.main.servlet.report;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.entity.HospitalDevice;
import com.mmp.main.util.Enums;

public class ViewDevicesTransferred extends BasicServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {

		int siteId = getSessionSiteId(request);
		int locationId = getSessionLocationId(request);

		String jpql = "SELECT h FROM HospitalDevice h WHERE h.site.id = ? AND h.status = ?";

		Map<Integer, Object> map = new HashMap<Integer, Object>();
		map.put(1, siteId);
		map.put(2, Enums.DEVICE_STATUS.TRANSFERRED.getStatus());

		if (locationId != 0) {
			jpql += " AND h.hospital.id = ?";
			map.put(3, locationId);
		}

		jpql += " ORDER BY h.hospital.id, h.hospitalDepartment.name, h.category.id, h.name";

		List<HospitalDevice> devices = dbQueryManager.queryJPQL(jpql, map, HospitalDevice.class);
		if (devices == null || devices.isEmpty()) {
			announceErorrMessage(request, response, "لم يتم  تسجيل معلومات نقل أي جهاز من خلال هذا النظام");
			return;
		}

		request.setAttribute("deviceList", devices);
		viewHTMLReport(request, response, "report/ViewDevicesTransferred.jsp",
				Enums.REPORT_TITLE.TRANSFERRED_DEVICES.getReportTitle());

	}

}
